package com.rms.hkpc.presenter;

import com.google.gson.reflect.TypeToken;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.db.dto.VersionDto;
import com.rms.hkpc.model.VersionModel;
import com.rms.hkpc.model.listener.OnResultListener;
import com.rms.hkpc.model.listener.OnResultProgressListener;
import com.rms.hkpc.view.IMainView;

import java.io.File;

public class MainPresenter extends BasePresenter<IMainView> {
    private VersionModel model = new VersionModel();
    public void getVersion(){
        model.getVersion(new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onGetVersionSuccess((ApiResultData<VersionDto>) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetVersionFails(e);
                }
            }
        },new TypeToken<ApiResultData<VersionDto>>(){}.getType());
    }

    public void downloadFile(String fileName, File file){
        model.download(fileName, file, new OnResultProgressListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onDownloadFileSuccess((File) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onDownloadFails(e);
                }
            }

            @Override
            public void onProgress(int progress) {
                if (getView() != null) {
                    getView().onDownloadProgress(progress);
                }
            }
        });
    }
}
